import {showMessage} from '../../js/my-plugin'
import {checkPassword, checkPhone} from '../../js/my-utils'

// 输出昵称错误信息
export const nicknameMsg = (_this, val) => {
  const length = val.length
  if (length < 2) {
    _this.warn[0].isError = true
    _this.warn[0].msg = '昵称过短'
    return
  }
  if (length > 16) {
    _this.warn[0].isError = true
    _this.warn[0].msg = '昵称过长'
    return
  }
  const regex = /[\s\\~`!@#$%^&*(){}|/=+?<>'";:]/
  if (regex.test(val)) {
    _this.warn[0].isError = true
    _this.warn[0].msg = '昵称不可包含-和_以外的特殊字符'
    return
  }
  _this.warn[0].isError = false
  _this.warn[0].msg = ''
  const data = {
    nickname: val
  }
  _this.$axios({
    method: 'post',
    url: '/api/user/check_nickname',
    data: _this.$qs.stringify(data)
  })
    .then((res) => {
      if (res.data.code === 200) {
        if (!res.data.data) {
          _this.warn[0].isError = true
          _this.warn[0].msg = '昵称已存在'
        }
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      console.log(error)
      showMessage('登陆失败，无法连接服务器', 0)
    })
}

// 输出密码错误信息
export const passwordMsg = (_this, val) => {
  const length = val.length
  if (length < 6) {
    _this.warn[1].isError = true
    _this.warn[1].msg = '密码不能小于6个字符'
    return
  }
  if (length > 16) {
    _this.warn[1].isError = true
    _this.warn[1].msg = '密码不能大于16个字符'
    return
  }
  if (!checkPassword(val)) {
    _this.warn[1].isError = true
    _this.warn[1].msg = '密码格式必须是字母+数字'
    return
  }
  _this.warn[1].isError = false
  _this.warn[1].msg = ''
}

// 输出手机号错误信息
export const mobileMsg = (_this, val) => {
  if (!checkPhone(val)) {
    _this.warn[2].isError = true
    _this.warn[2].msg = '请输入正确手机号'
    return
  }
  _this.warn[2].isError = false
  _this.warn[2].msg = ''
}

// 输出验证码错误信息
export const smsCodeMsg = (_this, val) => {
  const regex = /[0-9]{6}/
  if (!regex.test(val)) {
    _this.warn[3].isError = true
    _this.warn[3].msg = '请输入正确验证码'
    return
  }
  _this.warn[3].isError = false
  _this.warn[3].msg = ''
}

// 发送验证码
export const sendSmsCode = (_this, mobile) => {
  if (!checkPhone(mobile)) {
    showMessage('请先填写正确手机号', 0)
    return
  }
  const data = {
    mobile: mobile,
    type: 'reg'
  }
  _this.$axios({
    method: 'post',
    url: '/api/user/sms',
    data: _this.$qs.stringify(data)
  })
    .then((res) => {
      showMessage(res.data.msg, 1)
    })
    .catch((error) => {
      console.log(error)
      showMessage(error, 1)
    })
  let s = 60
  _this.sendDis = true
  const timer = setInterval(() => {
    s--
    _this.sendText = '重新发送（ ' + s + ' s ）'
    if (s === 0) clearInterval(timer)
  }, 1000)
  setTimeout(() => {
    _this.sendText = '重新发送'
    _this.sendDis = false
  }, s * 1000)
}

export const reg = (_this) => {
  const nickname = _this.form.nickname
  const password = _this.form.password
  const mobile = _this.form.mobile
  const smsCode = _this.form.smsCode
  if (nickname === '' || password === '' || mobile === '' || smsCode === '') {
    showMessage('请先完整填写信息', 0)
    return
  }
  if (_this.warn[0].isError || _this.warn[1].isError || _this.warn[2].isError || _this.warn[3].isError) {
    return
  }
  const data = {
    nickname: nickname,
    password: password,
    mobile: mobile,
    smsCode: smsCode
  }
  _this.$axios({
    method: 'post',
    url: '/api/user/reg',
    data: _this.$qs.stringify(data)
  })
    .then((res) => {
      if (res.data.code === 200) {
        showMessage(res.data.msg, 1)
        _this.$router.push('/login')
      } else {
        showMessage(res.data.msg, 0)
      }
    })
    .catch((error) => {
      console.log(error)
      showMessage(error, 0)
    })
}
